/*
 * @Author: LiJia
 * @Date: 2021-03-09 14:26:13
 * @LastEditTime: 2021-04-05 14:47:53
 * @LastEditors: LiJia
 * @Description: Description
 * @FilePath: \class-manage-system\miniprogram\pages\mine\children\my-course\my-course.ts
 */
Page({
  data: {
    clsUserInfo: {},
    courseTable: [],
    joinCourseModalConfig: {
      title: '添加课程',
      visible: false,
      placeholder: '请输入12位课程码',
      tips: '提示：课程码是课程唯一的标识码，如果没有请咨询对应老师。',
      maxlength: 12
    },
    joinCourseFormData: {
      courseID: ""
    },
    hasMoreCourseInfo: false
  },
  onLoad() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('clsUserInfo', (req) => {
      this.setData({
        clsUserInfo: req.data
      })
    })
    this.getCourseTable()
  },

  // 用户自定义方法

  /**
   * 获取课程表
   */
  getCourseTable() {
    let that = this
    const apiUrl = wx.getStorageSync('apiUrl')
    wx.request({
      url: apiUrl + '/course-select',
      data: {
        id: (this.data.clsUserInfo as any).id,
        role: (this.data.clsUserInfo as any).role
      },
      success: (res) => {
        let _res = res.data as any
        if (_res.code === 200) {
          that.setData({
            courseTable: _res.data as never
          })
        } else if (_res.code === 204) {
          that.setData({
            courseTable: []
          })
        } else {
          wx.showToast({
            title: _res.msg,
            icon: 'none',
            duration: 2021
          })
        }
      }
    })
  },

  /**
   * 打开添加课程弹窗
   */
  openAddCourseModal() {
    let modalVisible = 'joinCourseModalConfig.visible'
    this.setData({
      [modalVisible]: true
    })
  },
  /**
   * 添加课程弹窗取消事件
   */
  handleModalCancel() {
    console.log('取消')
    let modalVisible = 'joinCourseModalConfig.visible'
    this.setData({
      [modalVisible]: false
    })
  },
  /**
   * 添加课程弹窗确定事件
   */
  handleModalConfirm() {
    console.log('确定')
    if (this.courseIDValidate(this.data.joinCourseFormData.courseID)) {
      
      let modalVisible = 'joinCourseModalConfig.visible'
      this.setData({
        [modalVisible]: false
      })
      // 添加课程 手动
      if (this.data.joinCourseFormData.courseID === 'CS0017WLGC00') {
        let newCourse =  {
          id: 'CS0017WLGC00',
          courseID: 'CS0017WLGC00',
          name: '网络工程',
          teacher: '柯珊耀',
        }
        let courseTable = this.data.courseTable
        courseTable.splice(0, 0, newCourse as never)
        this.setData({
          courseTable: courseTable
        })
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '课程码无效',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: '课程ID不能为空！',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 获取输入框中的输入值
   */
  getInputCourseID(event: any) {
    let courseID = 'joinCourseFormData.courseID'
    this.setData({
      [courseID]: event.detail.value.toUpperCase()
    })
  },
  /**
   * 校验课程ID
   */
  courseIDValidate(courseID: String) {
    let valid = false
    if (courseID.length <= 0) {
      valid = false
    } else {
      valid = true
    }
    return valid
  },
  /**
   * 退出课程
   */
   exitCourse(e: any) {
    // e.detail.id
    // console.log(e)
    debugger
    let courseTable = this.data.courseTable
    for (let i = 0; i < courseTable.length; i++) {
      if ((courseTable[i] as any).id === e.detail.id) {
        courseTable.splice(i, 1)
        break
      }
    }
    this.setData({
      courseTable: courseTable
    })
   }
})